// SettlementController.java
package com.cddx.logistics.controller;

import com.cddx.logistics.domain.dto.SettlementQueryDTO;
import com.cddx.logistics.domain.vo.SettlementVO;
import com.cddx.logistics.service.SettlementService;
import com.cddx.logistics.util.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/settlement")
@Api(tags = "计划结算控制器")
@Slf4j

public class SettlementController {

    @Resource
    private SettlementService settlementService;

    @GetMapping("/queryPage")
    @ApiOperation("分页查询结算信息")
    public R queryPage(SettlementQueryDTO queryDTO) {
        return R.ok(settlementService.queryPage(queryDTO));
    }

    @PostMapping("/updateState")
    @ApiOperation("更新结算状态")
    public R updateSettlementState(@RequestParam Integer id,
                                   @RequestParam Integer settlementState) {
        boolean success = settlementService.updateSettlementState(id, settlementState);
        return success ? R.ok("状态更新成功") : R.error("状态更新失败");
    }
    // 在SettlementController中添加
    @GetMapping("/editPage")
    @ApiOperation("编辑页面")
    public String editPage(@RequestParam Integer id, Model model) {
        SettlementVO settlement = settlementService.getById(id);
        model.addAttribute("settlement", settlement);
        return "settlement/edit"; // 对应模板路径
    }

    @PostMapping("/update")
    @ApiOperation("更新状态")
    public R update(SettlementVO settlementVO) {
        try {
            // 验证数据
            if (settlementVO.getId() == null) {
                return R.error("ID不能为空");
            }

            boolean success = settlementService.updateState(settlementVO);
            if (success) {
                return R.ok("状态更新成功");
            } else {
                return R.error("状态更新失败，请检查数据");
            }
        } catch (Exception e) {
            log.error("更新状态失败", e);
            return R.error("系统错误: " + e.getMessage());
        }
    }
    @PostMapping("/delete")
    @ApiOperation("删除结算记录")
    public R delete(@RequestParam Integer id) {
        try {
            boolean success = settlementService.deleteById(id);
            if (success) {
                return R.ok("删除成功");
            } else {
                return R.error("删除失败，记录不存在或已被删除");
            }
        } catch (Exception e) {
            log.error("删除结算记录失败", e);
            return R.error("删除失败: " + e.getMessage());
        }
    }

}
